// Copyright 2023 Huawei Cloud Computing Technology Co., Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef __REDIS_JSON_H__
#define __REDIS_JSON_H__

#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>

#include "CommonMaster.h"
#include "securec.h"

namespace master {

const uint32_t MAX_JSON_NUM = 9;
const uint32_t MAX_STR_LENGTH = 64;

typedef enum {
    _eINVAILD,
    _eINT,
    _eSTRING,
    _eUINT64,

} VAL_TYPE_E;

typedef struct JSON_VAL_S {
    int iVal;
    char sVal[MAX_STR_LENGTH];
    uint64_t fwVal;
} JSON_VAL_T;

typedef enum {
    REDIS_IDLE = 0,
    REDIS_WORKER_CAP,

} REDIS_KEY_TYPE_E;

typedef struct JSON_STR_NAME_S {
    REDIS_KEY_TYPE_E keyType;

    std::string sKey[MAX_JSON_NUM];

} JSON_STR_NAME_T;

typedef struct JSON_VAL_TYPE_S {
    REDIS_KEY_TYPE_E keyType;
    VAL_TYPE_E valType[MAX_JSON_NUM];

} JSON_VAL_TYPE_T;

typedef struct JSON_INFO_MG_S {
    REDIS_KEY_TYPE_E keyType;
    struct {
        shared_ptr <WorkContextMgT> gWorkerCapInfo;
    } data;
} JSON_INFO_MG_T;


class RapidJson {

public:
    RapidJson();

    ~RapidJson();

    void rapidJsonParseString(std::string &str, REDIS_KEY_TYPE_E redisKey, shared_ptr <WorkContextMgT> &pWorkerCapInfo);

    void rapidJsonCreateStringWorkerCap(shared_ptr <WorkContextMgT> pWorkerCapInfo, std::string &sWorkerCap);
private:

    void rapidJsonParseWorkerCap(shared_ptr <WorkContextMgT> pWorkerCapInfo, JSON_VAL_T *pJsonVal, int idx);

    void rapidJsonParseGetData(shared_ptr <JSON_INFO_MG_T> pJsonData, JSON_VAL_T *pJsonVal, REDIS_KEY_TYPE_E redisKey,
                               int idx);

    bool rapidJsonParseContent(rapidjson::Document &document, std::string key, JSON_VAL_T *pJsonVal, VAL_TYPE_E valType);
};
};
#endif